package com.alexgaoyh.MutiModule.Dubbo.RWSeperate.provider.read.dataAuthority.aspect;

import java.util.List;
import java.util.Map;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.provider.read.dataAuthority.annotation.DataAuthorityClassAnnotation;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.provider.read.dataAuthority.aspect.test.vo.DictDictionaryVOUtilss;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.provider.read.dataAuthority.aspect.test.vo.SqlModelVO;

@Aspect
public class DataAuthorityAspect {

	@Around("@annotation(com.alexgaoyh.MutiModule.Dubbo.RWSeperate.provider.read.dataAuthority.annotation.DataAuthorityMethodAnnotation)")
	public Object dataAuthorityAnnotation(ProceedingJoinPoint pjp) throws Throwable {

		Object[] inputObjectArray = pjp.getArgs();
		// 判断这个类上面的注释是否是AnnotationName这个自定义的注解，如果是返回这个注解，如果不是返回null
		if (pjp.getTarget().getClass().getAnnotation(DataAuthorityClassAnnotation.class) != null) {

			System.out.println("class name: " + pjp.getSignature().getDeclaringType().getName());
			System.out.println("method name: " + pjp.getSignature().getName());

			// 获取到这个类上的注解
			DataAuthorityClassAnnotation anns = pjp.getTarget().getClass()
					.getAnnotation(DataAuthorityClassAnnotation.class);
			// 输出这个类上的注解的值
			System.out.println("注释在实现类上的annotation：" + anns.className());

			for (int i = 0; i < inputObjectArray.length; i++) {
				Object _obj = inputObjectArray[i];
				System.out.println("parameters: " + _obj);
				if (_obj instanceof Map) {
					Map<Object, Object> map = (Map<Object, Object>) _obj;
					List<SqlModelVO> _sqlModelVOList = DictDictionaryVOUtilss.operationMap.get(anns.className());
					if (_sqlModelVOList != null && _sqlModelVOList.size() > 0) {
						for (SqlModelVO sqlModelVO : _sqlModelVOList) {
							map.put(sqlModelVO.getProp(), sqlModelVO.getValues());
						}
						inputObjectArray[i] = map;
					}
				}
			}
			System.out.println("end of exec function");
		}

		Object result = pjp.proceed(inputObjectArray);

		return result;
	}

}
